home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 25 / CU Amiga Magazine's Super CD-ROM 25 (1998)(EMAP Images)(GB)(Track 1 of 2)[!][issue 1998-08].iso / CUCD / Magazine / C_Tutorial / Part-11 / req1 / loadsave.c < prev    next >
C/C++ Source or Header  |  1998-04-05  |  5KB  |  172 lines

  1. #include "loadsave.h"
  2. #include "bitmap.h"
  3. #include "drawwin.h"
  4. #include "gui.h"
  5. #include "main.h"
  6.  
  7. #include "iff.h"
  8.  
  9. #include<libraries/asl.h>
  10. #include<intuition/intuition.h>
  11. #include<workbench/workbench.h>
  12.  
  13. #include<string.h>
  14. #include<stdio.h>
  15.  
  16. #include<clib/asl_protos.h>
  17. #include<clib/dos_protos.h>
  18. #include<clib/graphics_protos.h>
  19. #include<clib/icon_protos.h>
  20.  
  21. /* Global handles for our requesters */
  22. static struct FileRequester* loadreq = NULL;
  23. static struct FileRequester* savereq = NULL;
  24.  
  25. /* Open an ASL load file requester */
  26. int load()
  27. {
  28.     /* Allocate the requester if we haven't already */
  29.     if(loadreq == NULL)
  30.         loadreq = (struct FileRequester*)
  31.             AllocAslRequestTags(ASL_FileRequest,
  32.                                                     ASLFR_TitleText,            "Load File",
  33.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS,
  34.                                                     ASLFR_InitialPattern,    "#?.iff",
  35.                                                     TAG_DONE);
  36.     if(loadreq)
  37.     {
  38.         struct Window* win = getDrawWin();
  39.         if(AslRequestTags(loadreq, ASLFR_Window, win, TAG_DONE))
  40.         {
  41.             char filename[MAXFILENAME];
  42.             /* Create complete filename from ASL's dir and file */
  43.             strcpy(filename, loadreq->rf_Dir);
  44.             if(AddPart(filename, loadreq->rf_File, MAXFILENAME))
  45.                 return loadfile(filename);
  46.             else
  47.                 printf("Error: could not make filename\n");
  48.         }
  49.         /* else: requester was cancelled */
  50.     }
  51.     else
  52.         printf("Error: could not allocate ASL (load) file request\n");
  53.     /* If we get this far then there was no fatal error */
  54.     return TRUE;
  55. }
  56.  
  57. int loadfile(char* filename)
  58. {
  59.     /* Record any fatal errors */
  60.     int going = TRUE;
  61.     IFFL_HANDLE handle;
  62.     /* Try to open the IFF file */
  63.     if(handle = IFFL_OpenIFF(filename, IFFL_MODE_READ))
  64.     {
  65.         UWORD colortable[256];
  66.         /* Get colour information */
  67.         LONG count = IFFL_GetColorTab(handle, colortable);
  68.         /* Get display information */
  69.         ULONG displayid = IFFL_GetViewModes(handle);
  70.         /* Get picture information */
  71.         struct IFFL_BMHD* bmhd = IFFL_GetBMHD(handle);
  72.         struct Window* win;
  73.         /* Try to adjust the screen to fit */
  74.         if(bmhd)
  75.         {
  76.             closeGUI();
  77.             /* If this fails, our local win will be NULL */
  78.             openGUI(bmhd->nPlanes, bmhd->w, bmhd->h, displayid);
  79.         }
  80.         if(win = getDrawWin())
  81.         {
  82.             /* Change screen colours */
  83.             LoadRGB4(&(win->WScreen->ViewPort), colortable, count);
  84.             /* If we can load the picture, update window's display */
  85.             if(IFFL_DecodePic(handle, getBitmap()))
  86.             {
  87.                 CopySBitMap(win->WLayer);
  88.                 setModified(FALSE);
  89.             }
  90.             else
  91.                 printf("Error: could not decode IFF picture\n");
  92.         }
  93.         else
  94.             going = FALSE;  /* The only fatal error */
  95.         IFFL_CloseIFF(handle);
  96.     }
  97.     else
  98.         printf("Error: could not open IFF file\n");
  99.     return going;
  100. }
  101.  
  102. /* Open an ASL save file requester */
  103. void save()
  104. {
  105.     /* Another way of saying "allocate if we haven't already" */
  106.     if(savereq ||
  107.         (savereq = (struct FileRequester*)
  108.             AllocAslRequestTags(ASL_FileRequest,
  109.                                                     ASLFR_TitleText,            "Save File",
  110.                                                     ASLFR_Flags1,                    FRF_DOPATTERNS | FRF_DOSAVEMODE,
  111.                                                     ASLFR_InitialPattern,    "#?.iff",
  112.                                                     ASLFR_InitialFile,        "picture.iff",
  113.                                                     TAG_DONE)))
  114.     {
  115.         struct Window* win = getDrawWin();
  116.         if(AslRequestTags(savereq, ASLFR_Window, win, TAG_DONE))
  117.         {
  118.             char filename[MAXFILENAME];
  119.             /* Create complete filename from ASL's dir and file */
  120.             strcpy(filename, savereq->rf_Dir);
  121.             if(AddPart(filename, savereq->rf_File, MAXFILENAME))
  122.             {
  123.                 /* Make sure our bitmap is the same as the display */
  124.                 SyncSBitMap(win->WLayer);
  125.                 /* Try saving our bitmap, using the screen's colours */
  126.                 if(IFFL_SaveBitMap(filename, getBitmap(),
  127.                                                     win->WScreen->ViewPort.ColorMap->ColorTable,
  128.                                                     IFFL_COMPR_BYTERUN1))
  129.                 {
  130.                     /* Write an icon for the file */
  131.                     struct DiskObject* dobj = GetDefDiskObject(WBPROJECT);
  132.                     setModified(FALSE);
  133.                     if(dobj)
  134.                     {
  135.                         /* Temporarily change the default tool */
  136.                         char* dtool = dobj->do_DefaultTool;
  137.                         dobj->do_DefaultTool = progName();
  138.                         /* Write out our icon */
  139.                         PutDiskObject(filename, dobj);
  140.                         /* Reinstate the default tool */
  141.                         dobj->do_DefaultTool = dtool;
  142.                         FreeDiskObject(dobj);
  143.                     }
  144.                 }
  145.                 else
  146.                     printf("Error: could not write IFF picture\n");
  147.             }
  148.             else
  149.                 printf("Error: could not make filename\n");
  150.         }
  151.         /* else: requester was cancelled */
  152.     }
  153.     else
  154.         printf("Error: could not allocate ASL (save) file request\n");
  155. }
  156.  
  157. /* Free any requesters that may have been allocated */
  158. void freeReqs()
  159. {
  160.     if(loadreq)
  161.     {
  162.         FreeAslRequest(loadreq);
  163.         loadreq = NULL;
  164.     }
  165.     if(savereq)
  166.     {
  167.         FreeAslRequest(savereq);
  168.         savereq = NULL;
  169.     }
  170. }
  171.  
  172.